package com.gla.giliair.task;

import com.gla.giliair.entity.MOrder;
import com.gla.giliair.entity.MOrderExample;
import com.gla.giliair.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/27 10:11
 */
@Configuration
@EnableScheduling
public class SaticScheduleTask {
    @Autowired
    private OrderService orderService;

    //每天0点执行一次操作
    @Scheduled(cron = "0 0 0 * * ?")
    //每天零点自动取消所有未支付的订单
    private void cancelOrderRegularly() {
        System.out.println("执行静态定时任务时间: " + LocalDateTime.now());
        List<Integer> stateList = new ArrayList<>();
        stateList.add(0);
        List<MOrder> orderList = orderService.selectOrdersByStates(stateList, new MOrderExample());
        if (!orderList.isEmpty()) {
            for (MOrder mOrder : orderList) {
                mOrder.setOrderState(-4);
                orderService.modifyOrder(mOrder);
            }
        }
    }
}
